<?php

namespace app\service;

use app\exception\ErrnoPlus;
use app\model\OssOrderDetailModel;
use share\exception\ApiException;
use share\service\BaseModelService;
use share\utils\Helper;

class OssOrderDetailService extends BaseModelService
{

    public static function getModel()
    {
        return new OssOrderDetailModel;
    }

    /**
     * 编辑条目 新增或者修改
     */
    public static function edit($info)
    {
        $id = $info['id'];
        unset($info['id']);
        if ($id == 0) {
            self::addOssOrderDetail($info);
        } else {
            self::updateOssOrderDetail($id, $info);
        }
        //TODO add code here after data change
        return $info;
    }

    /**
     * 新增条目
     */
    public static function addOssOrderDetail($info)
    {
        //TODO add code here  
        self::add($info);
    }

    /**
     * 修改条目
     */
    public static function updateOssOrderDetail($id, $info)
    {
        $origin  = self::getBase($id);
        if (empty($origin)) {
            throw new ApiException("origin data is not exists", ErrnoPlus::COMMON_INVALID_INPUT);
        }
        //TODO  add code here  

        self::update($id, $info);
    }

    /**
     * 删除条目
     */
    public static function delById($id)
    {
        $origin = self::getBase($id);
        if (empty($origin)) {
            return;
        }
        //TODO  add code here  
        $res = self::del($id);
        return $res;
    }

    /**
     * 页面查询条件
     */
    public static function buildCondition($filter)
    {
        $res = [];
        if (isset($filter['name']) && !empty($filter['name'])) {
            $res['name'] = ['like', '%' . $filter['name'] . '%'];
        }
        if (isset($filter['type']) && !empty($filter['type'])) {
            $res['type'] = $filter['type'];
        }
        //TODO  add code here  
        return $res;
    }

    public static function getDetailInfo($params){
        $res = self::orderDetailDefaultData();
        // 用户可以设置的属性
        $allowKeys = ['enable_default_plan','pre_record_duration','record_duration','event_record_pro_long','max_record_file_duration'];
    
        $given = Helper::arrayUnderscoreToCamelKey($params);
        foreach($allowKeys as $key){
            if(isset($given[$key])){
                $res[$key] = $given[$key];
            }
        }
        return $res;

    }

    public static function orderDetailDefaultData(){
        return [
            "enable_default_plan" => 1,
            "immediate_use" => 1,
            "pre_record_duration" => 5,
            "record_duration" => 10,
            "event_record_pro_long" => 1,
            'max_record_file_duration' => 1800,
            'user_id' => '',
            // 'device_no_owner' => '',
        ];
    }

}
